package com.house.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.house.entity.Admin;
import com.house.entity.PO.UsersPO;
import com.house.entity.response.UserData;
import com.house.utils.ErrCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.house.entity.House;
import com.house.entity.Users;
import com.house.service.IHouserService;
import com.house.service.IUserService;

import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private IUserService service;
    @Autowired
    private IHouserService dao;

    @RequestMapping("/toUserSystem")
    public String toUserSystemPage() {
        return "customer.jsp";
    }

    @RequestMapping("/toUserRentalPage")
    public String toUserRentalPage() {
        return "myrental.jsp";
    }

    @RequestMapping("/welcome")
    public String toWelcomePage() {
        return "welcome.jsp";
    }

    @RequestMapping("/toUpdateHousePage")
    public String toUpdatePage(int hID, HttpServletRequest request) {
        House house = dao.findHouseDetailsById(hID);
        request.getSession().setAttribute("House", house);
        return "updatehouse.jsp";
    }

    @RequestMapping("/updateUserPwd")
    @ResponseBody
    public String updateUserPwd(String id, String newPwd, String oldPwd) {
        Users oldUser = new Users();
        oldUser.setuID(Integer.parseInt(id));
        oldUser.setuPassword(oldPwd);
        Users checkUser = service.checkOldPwd(oldUser);
        if (checkUser != null) {
            Users newUser = new Users();
            newUser.setuID(Integer.parseInt(id));
            newUser.setuPassword(newPwd);
            int n = service.updateUserPwd(newUser);
            if (n > 0) {
                return "OK";
            }
        }
        return "FAIL";
    }

    /**
     * 通过房东id查询旗下租客
     *
     * @param landLord
     * @return
     */
    @RequestMapping("/getCustomByLandLord")
    @ResponseBody
    public UserData getCustomByLandLord(int landLord) {
        UserData userData = new UserData();

        if (landLord == 0) {
            userData.setCode(ErrCode.RESP_SERVICE_ERROR.getCode());
            userData.setMsg("landLord is zero");
            return userData;
        }
        UsersPO usersPO = new UsersPO();
        usersPO.setLandlord(landLord);
        List<Users> customs = new ArrayList<>();
        customs = service.findUserByCondition(usersPO);

        userData.setCount(customs.size());
        userData.setData(customs);
        userData.setCode(ErrCode.RESP_SUCCESS.getCode());
        userData.setMsg(ErrCode.RESP_SUCCESS.getMessage());
        return userData;

    }

    /**
     * 通过昵称模糊匹配租客
     *
     * @param name
     * @return
     */
    @RequestMapping("/getCustomByName")
    @ResponseBody
    public UserData getCustomByName(String name) {
        UserData userData = new UserData();

        UsersPO usersPO = new UsersPO();
        usersPO.setuName(name);
        List<Users> customs = new ArrayList<>();
        customs = service.findUserByCondition(usersPO);

        userData.setCount(customs.size());
        userData.setData(customs);
        userData.setCode(ErrCode.RESP_SUCCESS.getCode());
        userData.setMsg(ErrCode.RESP_SUCCESS.getMessage());
        return userData;
    }

    /**
     * 修改用户资料
     *
     * @param users
     * @param session
     * @return
     */
    @RequestMapping("/update_user_info")
    @ResponseBody
    public String updateUserInfo(Users users, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("Admin");
        if (admin == null) {
            return "FAIL";
        }

        users.setLandlord(admin.getId());

        int n = service.updateUserInfo(users);
        if (n>0){
            return "OK";
        }
        return "FAIL";
    }
}
